package com.zhuoyue.elmt.crypto;

import com.zhuoyue.elmt.crypto.SHA1;

import java.util.Random;

/**
 * 维保信息
 * 使用安全哈希算法SHA1对排序后的(约定的token、时间戳、随机字符串)字符串进行加密得到的结果
 */
public class CryptUtils {

    public static String decrypt(String token, String timestamp) {
        // 验证安全签名
        String nonce = genRandomStr();

        String signature = SHA1.gen(token, timestamp, nonce);
        return signature;
    }

    /**
     * 随机生成16位字符串
     */
    private static String genRandomStr() {
        String base = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
        Random random = new Random();
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < 16; i++) {
            int number = random.nextInt(base.length());
            sb.append(base.charAt(number));
        }
        return sb.toString();
    }

    public static void main(String[] args) {
        System.out.println(CryptUtils.decrypt("12312adsaf", "2017-04-05"));
    }

}
